//
//  SchoolListViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/30.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "SchoolListViewController.h"
#import "SchoolListTableViewCell.h"
#import "SchoolListModel.h"
#import "SearchListViewController.h"
#import "HttpRequest.h"
#import <UIImageView+WebCache.h>
#import "NormalWebViewController.h"
#import <MJRefresh.h>
#import "LocationModel.h"

@interface SchoolListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSMutableArray <SchoolListModel *> *dataArr;
@property (strong, nonatomic) MetaModel *model;
@end

@implementation SchoolListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupView];
    [self loadData];
}

- (void)setupView {
    
    self.navigationItem.title = @"精品学校";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:0];
    [button setImage:[UIImage imageNamed:@"searchIcon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 44, 44);
    UIView *view = [[UIView alloc] initWithFrame:button.bounds];
    [view addSubview:button];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight - XNavigationHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorInset = UIEdgeInsetsMake(0, 16, 0, 0);
    [_tableview registerNib:[UINib nibWithNibName:@"SchoolListTableViewCell" bundle:nil] forCellReuseIdentifier:@"SchoolListTableViewCell"];
    [self.view addSubview:_tableview];
    
    _tableview.separatorInset = UIEdgeInsetsMake(0, 16, 0, 0);
    _tableview.tableFooterView = [UIView new];
    _tableview.footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadData {
    
    NSDictionary *para = nil;
    if ([LocationModel shared].areaNumber.length > 0) {
        para = @{@"area": [LocationModel shared].areaNumber};
    }
    __weak typeof(self) weakself = self;
    self.dataArr = [NSMutableArray array];
    [HttpRequest getURL:kschool_list paras:para showProgress:NO successful:^(id  _Nullable result) {
        
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            SchoolListModel *model = [[SchoolListModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage < model.pageCount) {
            weakself.model = model;
        }else {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }
    } failed:nil];
}

- (void)loadMoreData {
    
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kschool_list paras:@{@"page": [NSNumber numberWithInt:self.model.currentPage + 1]} showProgress:NO successful:^(id  _Nullable result) {
        
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            SchoolListModel *model = [[SchoolListModel alloc] initWithDic:dic];
            [weakself.dataArr addObject:model];
        }
        [weakself.tableview reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage < model.pageCount) {
            weakself.model = model;
            [weakself.tableview.footer endRefreshing];
        }else {
            [weakself.tableview.footer endRefreshingWithNoMoreData];
        }
    } failed:^(int code, NSString *errMsg) {
        [weakself.tableview.footer endRefreshing];
    }];
}

- (void)searchButtonPressed {
    
    UINavigationController *navi = [SearchListViewController searchController:searchClassSchool];
    [self.navigationController presentViewController:navi animated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identity = @"SchoolListTableViewCell";
    SchoolListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(SchoolListTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SchoolListModel *model = _dataArr[indexPath.row];
    [cell.schoolImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    cell.nameLabel.text = model.name;
    cell.addressLabel.text = [NSString stringWithFormat:@"%@%@", model.region, model.address_detail];
    cell.starView.value = model.grade;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NormalWebViewController *controller = [[NormalWebViewController alloc] init];
    controller.url = _dataArr[indexPath.row].link;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
